package com.bbx.taxi.client.Activity.Order;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.gclient.GMsg;
import com.bbx.api.sdk.model.passanger.OrderDetailBuild;
import com.bbx.api.sdk.model.passanger.OrderEvaluate;
import com.bbx.api.sdk.model.passanger.Return.CarType.CarType;
import com.bbx.api.sdk.model.passanger.Return.Jmsg.Msgs;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.api.sdk.model.passanger.Return.OrderDetails;
import com.bbx.api.sdk.model.passanger.Return.PriceDetail;
import com.bbx.api.sdk.model.passanger.Return.ShareDetail;
import com.bbx.api.sdk.model.passanger.Return.UsableSale;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.OrderDetailNet;
import com.bbx.api.sdk.net.passeger.conn.OrderEvaluateNet;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Adapter.PayAdapter;
import com.bbx.taxi.client.Adapter.PayAdapter.ViewHolder;
import com.bbx.taxi.client.Adapter.PayDetailsAdapter;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute.CjType;
import com.bbx.taxi.client.Bean.Extra.OrderMsg;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Task.MyOrderPayTask;
import com.bbx.taxi.client.Util.AgainOrder;
import com.bbx.taxi.client.Util.CouponUtils;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.Util.FormatUtil;
import com.bbx.taxi.client.Util.ListViewHeight;
import com.bbx.taxi.client.Util.NetworkDetector;
import com.bbx.taxi.client.Util.RequestUtils;
import com.bbx.taxi.client.Util.Tel;
import com.bbx.taxi.client.Util.ToCity2;
import com.bbx.taxi.client.Util.VersionUtils;
import com.bbx.taxi.client.emoji.MyConvertToMsg;
import com.bbx.taxi.client.widget.Dailog.MyCouponDailog;
import com.bbx.taxi.client.widget.Dailog.MyCouponDailog.onClickRightListener;
import com.bbx.taxi.client.widget.Drag.BaseDragLayout.OnDragViewStatusListener;
import com.bbx.taxi.client.widget.Drag.DragLayout;
import com.bbx.taxi.client.widget.MaxByteLengthEditText;
import com.bbx.taxi.client.widget.SlidingCard2.DiscoverContainerView;
import com.bbx.taxi.client.widget.SlidingCard2.DiscoverContainerView.OnFinish;
import com.bbx.taxi.client.widget.pop.SharePopView;
import com.bbx.taxi.client.widget.pop.SharePopView.OnShareDetaiFinish;
import com.bbx.taxi.client.wxapi.WXPayEntryActivity;
import com.umeng.socialize.sso.UMSsoHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import butterknife.Bind;


/**
 * 订单评价
 * 
 */
public class OrderEvaluationActivity2 extends BaseBBXActivity implements
		OnDragViewStatusListener, OnShareDetaiFinish,
		MyOrderPayTask.OnPayFinishListener {
	/** 待评价状态 */
	public static final int STATUS_EVA = 0;
	/** 正在评价状态 */
	public static final int STATUS_EVAING = 1;
	/** 完成状态 */
	public static final int STATUS_FINISH = 2;
	/** 支付状态 */
	public static final int STATUS_PAY = 3;
	/**空*/
	public static final int STATUS_NULL = 4;
	// 第三方支付平台的优惠
	public String DISCOUNT_YINLIAN = "0";
	public String DISCOUNT_YUE = "0";
	public String DISCOUNT_ALIPAY = "0";
	public String DISCOUNT_WEIXIN = "0";
	public String DISCOUNT_LINE = "0";
	@Bind(R.id.layout_main)
	RelativeLayout layout_main;
	@Bind(R.id.dragLayout)
	public DragLayout dragLayout;
	@Bind(R.id.test)
	View test;
	@Bind(R.id.listview_pay)
	ListView listview_pay;
	/** 费用明细列表视图 */
	@Bind(R.id.lv_paydetail)
	ListView listview_paydetail;
	@Bind(R.id.tv_cost_detail)
	TextView tv_cost_detail;
	@Bind(R.id.include_wait)
	LinearLayout include_wait;
	@Bind(R.id.contentview)
	DiscoverContainerView contentView;
	/** 司机名称 */
	@Bind(R.id.tv_drivername)
	TextView tv_drivername;
	/** 车牌号 */
	@Bind(R.id.tv_car_no)
	TextView tv_car_no;
	/** 需支付金额 */
	@Bind(R.id.tv_pay)
	TextView tv_pay;
	/** 内容 */
	@Bind(R.id.include_cost)
	LinearLayout layout_cost;
	/** 费用明细的具体内容，该布局替换成另外一个视图出行，暂不可用 */
	@Bind(R.id.layout_cost_detail)
	LinearLayout layout_cost_detail;
	/** 提示语 */
	@Bind(R.id.layout_anonymous)
	RelativeLayout layout_anonymous;
	@Bind(R.id.et_message)
	MaxByteLengthEditText et_message;
	@Bind(R.id.et_message2)
	TextView et_message2;
	/** 待评价布局 */
	@Bind(R.id.layout_evaluate)
	LinearLayout layout_evaluate;
	/** 再次下单 */
	@Bind(R.id.btn_confirm)
	RelativeLayout btn_confirm;
	@Bind(R.id.layout_confirm)
	LinearLayout layout_confirm;
	/** 再次下单 TextViewq */
	@Bind(R.id.tv_confirm)
	TextView tv_confirm;
	/** 评价 */
	@Bind(R.id.rb_rating_s)
	RatingBar rb_rating_s;
	@Bind(R.id.tv_rating)
	TextView tv_rating;
	@Bind(R.id.rb_rating_b)
	RatingBar rb_rating_b;
	/** 显示需支付和已支付 */
	@Bind(R.id.tv_need_pay)
	TextView tv_need_pay;
	@Bind(R.id.layout_payway)
	RelativeLayout layout_payway;
	/** hint支付类型 */
	@Bind(R.id.layout_hint_paytype)
	RelativeLayout layout_hint_paytype;
	@Bind(R.id.tv_hint_paytype)
	TextView tv_hint_paytype;
	/** 我要优惠劵 */
	@Bind(R.id.btn_coupon)
	Button btn_coupon;
	/** 阴影 */
	@Bind(R.id.pop_background)
	public View pop_background;
	@Bind(R.id.ib_sj)
	ImageButton ib_sj;
	@Bind(R.id.text_offline_payment)
	TextView text_offline_payment;
	/** 订单Id列表 */
	ConcurrentLinkedQueue<String> dataList = new ConcurrentLinkedQueue<String>();
	/** 选择支付类型列表 */
	ConcurrentLinkedQueue<Integer> payList = new ConcurrentLinkedQueue<Integer>();
	/** 界面状态 */
	ConcurrentLinkedQueue<Integer> statusList = new ConcurrentLinkedQueue<Integer>();
	/** 是否刷新成功 */
	ConcurrentLinkedQueue<Boolean> successList = new ConcurrentLinkedQueue<Boolean>();
	/** 是否刷新成功 */
	ConcurrentLinkedQueue<MyOrderList> myOrderListList = new ConcurrentLinkedQueue<MyOrderList>();
	private int order_type;
	private int viewId;
	/** 订单Id集合 */
	private List<String> lits_order_id = new ArrayList<String>();
	/** 界面状态，最下面的那个页面的，主要用于传递到卡片上去。卡片根据状态显示样本。评价状态，0待评价，1正在评价，2已评价 ,3待支付 */
	public int ym_status = STATUS_PAY;
	/** 评价状态，1待评价，2已评价 */
	public int eva_status = Value.VALUATE_STATUS_NO;
	private MyOrderList mMyOrderList;
	private MyOrderDB db_myorder;// 我的订单数据库
	private MyOrderingDB db_myordering;// 我的订单数据库
	private OrderEvaluateTask mOrderEvaluateTask;
	private OrderDetailTask mOrderDetailTask;
	private boolean isonNotify = false;
	private boolean isTouch = true;
	private String order_id;
	/** 支付列表 */
	private List<Map<String, String>> list;
	/** 支付费用明细列表 */
	private List<Map<String, String>> list_paydetail = new ArrayList<Map<String, String>>();
	/** 支付费用明细列表【更加详细，及另外一个界面】 */
	private List<Map<String, String>> list_paydetail_more = new ArrayList<Map<String, String>>();
	private PayAdapter mPayAdapter;
	private PayDetailsAdapter mPayDetailsAdapter;
	// 支付id,则为类型，0为银联
	private int pay_id = 0;
	/** 需要支付的金额，不包括第三方支付的优惠 */
	private boolean isSuccess = false;
	/** 费用详情的popview */
	private PopupWindow popview;
	/** 车费总计 */
	private double total_fare;
	/** 车费 ,单位元*/
	private double fare;
	/** 过路费 */
	private double highwey_fee = 0;
	/** 优惠劵,单位元 */
	private double coupon;
	/** 需要支付,单位元 */
	private double pay;
	/** 第三方支付优惠 */
	private double discount;
	/** 分享POP */
	public SharePopView mSharePopView;
	/** 用于判断是否执过，setView里的setStatus，执行过了，则不在调用 */
	public boolean isViewAndStatus = false;
	/** 第一次出现加载框的订单详情， */
	public boolean FirstLoad = true;
	private Intent intent;
	/** 支付方式 */
	private String pay_gate;
	/** 优惠大礼包 */
	private MyCouponDailog coupondialog;
	int down_y = 0;
	boolean isPaySuccuss=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		jmsg_data = "";
		setContentView(R.layout.activity_orderevaluation2);
		super.onCreate(savedInstanceState);
		MyOrderPayTask.setOnPayFinishListener(OrderEvaluationActivity2.this);
	}

	public void onSetTitle(int ym_status) {
		this.ym_status=ym_status;
		this.ym_status = ym_status;
		switch (getYMStatus()) {
		// 支付状态
		case STATUS_PAY:
			setTitle(true, getString(R.string.title_pay), -1,
					getString(R.string.complaint));
			break;
		// 待评价状态
		case STATUS_EVA:
			setTitle(true, getString(R.string.title_pay_finish), -1,
					getString(R.string.complaint));
			break;
		// 正在评价状态
		case STATUS_EVAING:
			setTitle(true, getString(R.string.title_pay_finish), -1,
					getString(R.string.complaint));
			break;
		// 完成状态
		case STATUS_FINISH:
			setTitle(true, getString(R.string.title_pay_finish), -1,
					getString(R.string.complaint));
			break;
		case STATUS_NULL:
			setTitle("");
			break;
		}
	}

	/**
	 * 注意事项，如果在这边分享了优惠大礼包，然后返回界面，则会刷新父类的订单列表，，这时候后台一定要有【过路费】这些信息
	 * 人，然后还要刷新下订单详情，，如果没有
	 * ，则去掉super.onResume();不刷新列表，，不然，，下个订单过来的时候，，由于没有【过路费】，则会报错
	 */
	@Override
	protected void onResume() {
		super.onResume();
		RequestUtils.requesBalance(this);
		getOrderDetail(order_id, false);
	}

	@Override
	protected void initView() {
		coupondialog = new MyCouponDailog(context);
		layout_main.setVisibility(View.GONE);
		onSetPayList();
		mSharePopView = new SharePopView(context, pop_background);
		mPayDetailsAdapter = new PayDetailsAdapter(context, list_paydetail);
		listview_paydetail.setAdapter(mPayDetailsAdapter);
		tv_cost_detail.setOnClickListener(this);
		dragLayout.setOnDragViewStatusListener(this);
		ib_sj.setOnClickListener(this);
		et_message.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView arg0, int arg1,
					KeyEvent event) {
				return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
			}
		});
		et_message.setMaxByteLength(60);
		et_message.setOnClickListener(this);
		btn_coupon.setOnClickListener(this);
		isonNotify = true;
		db_myorder = new MyOrderDB();
		db_myordering = new MyOrderingDB();
		layout_evaluate.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int move_y = 0;
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					down_y = (int) event.getY();
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					move_y = (int) event.getY();
					if ((move_y - down_y) > 50) {
						if (getYMStatus() == STATUS_EVAING) {
							setYMStatus(STATUS_EVA);
						}
					}
				}
				return false;
			}
		});
		intent = getIntent();
		final String order_id = intent.getExtras().getString(
				OrderMsg.order_id_finish);
		this.order_id = order_id;

		if (order_id == null || order_id.equals(""))
			finish();
		try {
		} catch (Exception e) {
		}
		mSharePopView.setOnShareDetaiFinish(OrderEvaluationActivity2.this);
		getOrderDetail(order_id, true);
	}

	@Override
	protected void initData() {

	}

	private void setView() {
		// 初始化评价状态
		if (mMyOrderList.getValuate_status() != null) {
			eva_status = Integer.parseInt(mMyOrderList.getValuate_status());
		}
		int order_status = mMyOrderList.getOrder_status();
		if (order_status == Value.ORDER_STATUS_OFFCAR) {
			setYMStatus(STATUS_PAY);
		} else {
			switch (eva_status) {
			case Value.VALUATE_STATUS_NO:
				setYMStatus(STATUS_EVA);
				break;
			case Value.VALUATE_STATUS:
				setYMStatus(STATUS_FINISH);
				break;
			}
		}
		if (!isViewAndStatus) {
			setYMStatus(ym_status);
			isViewAndStatus = true;
		}
		lits_order_id.add(order_id);
		tv_drivername.setText(mMyOrderList.getDriver_name());
		tv_car_no.setText(mMyOrderList.getCar_NO());
		// tv_time.setText(DateFormat.onDateRemove_ss(mMyOrderList
		// .getAppoint_time()));
		MainAttribute main=MainAttribute.getInstance();
		order_type = mMyOrderList.getOrder_type();
		switch (order_type) {
		case Value.ORDER_TYPE_SN:
			// tv_order_type.setText(R.string.sn);
			viewId = Value.VIEWID_SN;
			// tv_count.setVisibility(View.GONE);
			break;
		case Value.ORDER_TYPE_KJ:
			// tv_order_type.setText(R.string.kj);
			viewId = Value.VIEWID_KJ;
			// tv_count.setText(String.format(getString(R.string.num_jian),
			// mMyOrderList.getLocations().getCount()));
			break;
		case Value.ORDER_TYPE_PC:
			// tv_order_type.setText(R.string.pc);
			// tv_count.setText(String.format(getString(R.string.num),
			// mMyOrderList.getLocations().getCount()));
			viewId = Value.VIEWID_PC;
			main.setCjType(CjType.PC);
			break;
		case Value.ORDER_TYPE_BC:
			// tv_order_type.setText(R.string.bc);
			// tv_count.setText(String.format(getString(R.string.num_car),
			// mMyOrderList.getLocations().getCount()));
			viewId = Value.VIEWID_PC;
			main.setCjType(CjType.BC);
			break;
		}

		// tv_start.setText(mMyOrderList.getLocations().getStart().getAddress());
		// tv_end.setText(mMyOrderList.getLocations().getEnd().getAddress());
		rb_rating_s.setRating(FormatUtil.onFormatStar((float) mMyOrderList
				.getDriver_star()));
		tv_rating.setText(FormatUtil.onFormatStar((float) mMyOrderList
				.getDriver_star()) + "");
		if (dataList.size() > 0) {
			contentView.setVisibility(View.VISIBLE);
			setOnCloseSwipeLayout(true);
		} else {
			setOnCloseSwipeLayout(false);
		}
		contentView.initCardView(OrderEvaluationActivity2.this, dataList,
				payList, statusList, successList, myOrderListList, isLine);
		contentView.setOnFinish(new OnFinish() {

			@Override
			public void onFinish() {
				setOnCloseSwipeLayout(false);
				onSetTitle(getYMStatus());

			}
		});
		btn_confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (tv_confirm.getText().toString()
						.equals(getString(R.string.btn_confirmation))) {
					getOrderDetail(order_id, true);
				} else {
					// 提交评价
					if (getYMStatus() == STATUS_EVAING) {
						// 提交评价
						OrderEvaluate mOrderEvaluate = new OrderEvaluate(
								context);
						mOrderEvaluate.uid = mApplication.getUid();
						mOrderEvaluate.access_token = mApplication.getToken();
						mOrderEvaluate.order_id = order_id;
						mOrderEvaluate.driver_id = mMyOrderList.driver_id;
						mOrderEvaluate.star = ((int) rb_rating_b.getRating()) * 20;
						mOrderEvaluate.suggest = new MyConvertToMsg()
								.parseEmoji(context, et_message.getText()
										.toString());
						mOrderEvaluate.problem = "";
						mOrderEvaluateTask = new OrderEvaluateTask(context,
								mOrderEvaluate);
						mOrderEvaluateTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
					}
					// 再次下单
					else if (getYMStatus() == STATUS_EVA
							|| getYMStatus() == STATUS_FINISH) {
						AgainOrder.onAgainOrder(OrderEvaluationActivity2.this, mMyOrderList, viewId);
					}
					// 支付
					else if (getYMStatus() == STATUS_PAY) {
						if (!isSuccess) {
							ToastUtil.showToast(context, R.string.no_network);
						} else {
							MyOrderPayTask.GATEWAY gateway = null;
							if (list.get(pay_id).get(PayAdapter.TYPE)
									.equals(PayAdapter.TYPE_YINLIAN)) {
							} else if (list.get(pay_id).get(PayAdapter.TYPE)
									.equals(PayAdapter.TYPE_ALIPAY)) {
								gateway=MyOrderPayTask.GATEWAY.alipay;
							} else if (list.get(pay_id).get(PayAdapter.TYPE)
									.equals(PayAdapter.TYPE_WEIXIN)) {
								gateway=MyOrderPayTask.GATEWAY.weixin;
							}else if (list.get(pay_id).get(PayAdapter.TYPE)
									.equals(PayAdapter.TYPE_LINE)) {
								gateway=MyOrderPayTask.GATEWAY.line;
							}else if (list.get(pay_id).get(PayAdapter.TYPE)
									.equals(PayAdapter.TYPE_YUE)) {
								gateway=MyOrderPayTask.GATEWAY.yue;
							}
							int int_pay=(int)(pay*100);
							RequestUtils.requesOrderPay(context,gateway,order_id,mMyOrderList.getDriver_id(),int_pay);
						}
					}
				}
			}
		});
		rb_rating_b.setIsIndicator(true);
		rb_rating_b.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (!isTouch)
					return true;
				switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						setYMStatus(STATUS_EVAING);
						break;
				}
				return false;
			}
		});
		layout_main.setVisibility(View.VISIBLE);
		onAixinActivity();
	}

	/**
	 * 设置支付列表
	 */
	private void onSetPayList() {
		list = new ArrayList<Map<String, String>>();
		Map<String, String> map = new HashMap<String, String>();
		if(Value.isNEW) {
		 // 余额
		 map.put(PayAdapter.TYPE, PayAdapter.TYPE_YUE);
		 map.put(PayAdapter.TEXT, getString(R.string.pay_way_yue));
		 map.put(PayAdapter.DISCOUNT, DISCOUNT_YUE);
		 list.add(map);
		}
		// 支付宝
		map = new HashMap<String, String>();
		map.put(PayAdapter.TYPE, PayAdapter.TYPE_ALIPAY);
		map.put(PayAdapter.TEXT, getString(R.string.pay_way_alipay));
		map.put(PayAdapter.DISCOUNT, DISCOUNT_ALIPAY);
		list.add(map);
		// 微信
		map = new HashMap<String, String>();
		map.put(PayAdapter.TYPE, PayAdapter.TYPE_WEIXIN);
		map.put(PayAdapter.TEXT, getString(R.string.pay_way_weixin));
		map.put(PayAdapter.DISCOUNT, DISCOUNT_WEIXIN);
		map.put(PayAdapter.AVAILABLE, WXPayEntryActivity
				.isWXAppInstalledAndSupported(context) ? "true" : "false");
		list.add(map);
		//现金
		map = new HashMap<String, String>();
		map.put(PayAdapter.TYPE, PayAdapter.TYPE_LINE);
		map.put(PayAdapter.TEXT, getString(R.string.pay_way_line));
		map.put(PayAdapter.DISCOUNT, DISCOUNT_LINE);
		list.add(map);
		

		discount = Double
				.parseDouble(list.get(pay_id).get(PayAdapter.DISCOUNT));
		mPayAdapter = new PayAdapter(context, list);
		listview_pay.setAdapter(mPayAdapter);
		ListViewHeight.setListViewHeightBasedOnChildren(listview_pay);
		listview_pay.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
									long arg3) {
				pay_id = position;
				try {
					discount = Double.parseDouble(list.get(position).get(
							PayAdapter.DISCOUNT));
					pay = pay - discount;
					if (pay < 0)
						pay = 0;
				} catch (Exception e) {
				}
				//看是否点击了现金支付，然后更加是否是线上支付专享，的价格
				//判断是否是线上支付专享
				JSONObject json;
				try {
					String paymode = "";
					if (mMyOrderList.getDiscount() != null && mMyOrderList.getDiscount().getMetadata() != null) {
						json = new JSONObject(mMyOrderList.getDiscount().getMetadata());
						paymode = json.getString("paymode");
					}
					list_paydetail.clear();
					// 添加车费总计
					addPayDetail(getString(R.string.total_fare),
							total_fare + "",
							PayDetailsAdapter.TYPE_PRICE, "", true);
					if (getYMStatus() == STATUS_PAY) {
						int sale_type = 0;
						double sale_amount = 0;
						try {
							sale_type = mMyOrderList.getPrice_Detail().getSaleType();
							sale_amount = mMyOrderList.getPrice_Detail().getSaleAmount();
						} catch (Exception e) {
						}
						if (CouponUtils.isMarkOnline(paymode) || sale_type == UsableSale.sale_type_online) {
							// 线上支付专享则不需要有优惠。需支付
							if (list.get(pay_id).get(PayAdapter.TYPE).equals(PayAdapter.TYPE_LINE)) {
								pay = Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice_Detail().getActual_price())) - discount;
							} else {
								//单位元
								double double_coupon;
								//如果是线上支付立减则，取sale_amount的值
								if (sale_type == UsableSale.sale_type_online) {
									double_coupon = Double.parseDouble(FormatUtil.onFormatPrice(sale_amount));
								} else {
									//如果是线上支付专享则取优惠劵	
									double_coupon = coupon;
								}
								pay = Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice_Detail().getActual_price())) - discount - double_coupon;
								// 添加优惠劵
								addCouponPayDetail(false);
							}
						} else {
							pay = Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice_Detail().getActual_price())) - discount;
							// 添加优惠劵
							addCouponPayDetail(false);
						}
					}
				} catch (JSONException e) {
				}
				/////////

				tv_pay.setText(FormatUtil.onFormatDecimal(pay));
				if (getYMStatus() == STATUS_PAY) {
					tv_confirm.setText(String.format(
							getString(R.string.confirm_pay), tv_pay.getText()));
				}
				ViewHolder vHollder = (ViewHolder) v.getTag();
				if (!vHollder.cb_pay.isChecked()) {
					vHollder.cb_pay.toggle();
				}
				for (int i = 0; i < list.size(); i++) {
					if (i == position) {
						mPayAdapter.isSelected.put(i, true);
					} else {
						mPayAdapter.isSelected.put(i, false);
					}
				}
				mPayDetailsAdapter.notifyDataSetChanged();
				ListViewHeight
						.setListViewHeightBasedOnChildren(listview_paydetail);
				mPayAdapter.notifyDataSetInvalidated();
			}
		});
	}

	private void addCardView(String order_id) {
		if((payList!=null&&payList.contains(order_id))||this.order_id.equals(order_id)){
			return;
		}
		myOrderListList.add(mMyOrderList);
		dataList.add(this.order_id);
		payList.add(pay_id);
		statusList.add(getYMStatus());
		successList.add(isSuccess);
		contentView.initCardView(OrderEvaluationActivity2.this, dataList,
				payList, statusList, successList, myOrderListList, isLine);
		isSuccess = false;
		this.order_id = order_id;
		// 重置支付选项
		pay_id = 0;
		for (int i = 0; i < list.size(); i++) {
			if (i == pay_id) {
				mPayAdapter.isSelected.put(i, true);
			} else {
				mPayAdapter.isSelected.put(i, false);
			}
		}
		mPayAdapter.notifyDataSetInvalidated();

		// 如果contentView数据为空，则add为加旧的，另外一个布局加新的，
		if (contentView.getVisibility() == View.GONE) {
			contentView.setVisibility(View.VISIBLE);
		}
		setOnCloseSwipeLayout(true);
		onScrollOrigin();

		// mMyOrderList赋值要在添加卡片后面
		mMyOrderList = db_myorder.onReadDB(order_id);
		lits_order_id.add(order_id);
		tv_drivername.setText(mMyOrderList.getDriver_name());
		// tv_time.setText(DateFormat.onDateRemove_ss(mMyOrderList
		// .getAppoint_time()));
		order_type = mMyOrderList.getOrder_type();
		switch (order_type) {
		case Value.ORDER_TYPE_SN:
			// tv_order_type.setText(R.string.sn);
			// tv_count.setVisibility(View.GONE);
			break;
		case Value.ORDER_TYPE_PC:
			// tv_order_type.setText(R.string.pc);
			// tv_count.setText(String.format(getString(R.string.num),
			// mMyOrderList.getLocations().getCount()));
			break;
		case Value.ORDER_TYPE_BC:
			// tv_order_type.setText(R.string.bc);
			// tv_count.setText(String.format(getString(R.string.num_car),
			// mMyOrderList.getLocations().getCount()));
			break;
		case Value.ORDER_TYPE_KJ:
			// tv_order_type.setText(R.string.kj);
			// tv_count.setText(String.format(getString(R.string.num_jian),
			// mMyOrderList.getLocations().getCount()));
			break;
		}
		// tv_start.setText(mMyOrderList.getLocations().getStart().getAddress());
		// tv_end.setText(mMyOrderList.getLocations().getEnd().getAddress());
		rb_rating_s.setRating(FormatUtil.onFormatStar((float) mMyOrderList
				.getDriver_star()));
		tv_rating.setText(FormatUtil.onFormatStar((float) mMyOrderList
				.getDriver_star()) + "");
		getOrderDetail(order_id, false);
		onRecovery();
	}

	/**
	 * 恢复界面原来的样子
	 */
	public void onRecovery() {
		setYMStatus(STATUS_PAY);
	}

	/**
	 * 获取订单详情
	 */
	public void getOrderDetail(String order_id, boolean isLoading) {
		if (!NetworkDetector.detect(context)){
			onSetTitle(STATUS_NULL);
			layout_main.setVisibility(View.GONE);
	        if (loaddialog != null) {
				loaddialog.onDismiss();
			}
			return;
		}
	
		if (order_id != null) {
			OrderDetailBuild mOrderDetail = new OrderDetailBuild(context);
			mOrderDetail.uid = mApplication.getUid();
			mOrderDetail.access_token = mApplication.getToken();
			mOrderDetail.order_id = order_id;
			mOrderDetailTask = new OrderDetailTask(context, mOrderDetail,
					isLoading);
			mOrderDetailTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	}

	/**
	 * 订单详情
	 */
	private class OrderDetailTask extends BaseAsyncTask {

		private OrderDetailBuild mOrderDetail;
		private boolean isLoading;

		public OrderDetailTask(Activity context, OrderDetailBuild mOrderDetail,
				boolean isLoading) {
			super(context, isLoading);
			this.isLoading = isLoading;
			this.mOrderDetail = mOrderDetail;
		}

		@Override
		public void onSuccess(int status,Object object) {
			isSuccess = true;
			if (object != null) {
				OrderDetails mOrderDetails = (OrderDetails) object;
				if (mOrderDetails != null) {
					mMyOrderList = mOrderDetails.getDetails();
					// //////////////////////////////////////////
					// 设置试图,只有isLoading=true的时候，才重新设置试图，，isLoading=true则是第一次启动和评价才有，FirstLoad为第一次启动加载框才有，，其他一概不重新设置试图
					if (isLoading && FirstLoad) {
						String city=mMyOrderList.getCity();
						if(city==null||city.equals("")){
							try{
							city= ToCity2.getCity(mMyOrderList.getLocations().getStart().getAddress());
							}catch (Exception e){}
						}
						mSharePopView.getShareDetai(Value.TYPE_SHARE,DateFormat.onDate_Long(mMyOrderList.getOrder_time()),city);
						FirstLoad = false;
						setView();

					}

					// 如果是确认中...却订单已经完成了，则跳转到待评价
					if (tv_confirm.getText().toString()
							.equals(getString(R.string.btn_confirmation))
							&& mMyOrderList.getOrder_status() == Value.ORDER_STATUS_FINISH) {
						onPaySuccuss();
					} else {
						// ////////////////////////
						PriceDetail price_detail = mMyOrderList
								.getPrice_Detail();
						// 车费
						if(price_detail!=null){
							fare =Double.parseDouble(FormatUtil.onFormatPrice(price_detail.getPrice()));
						}else{
							fare =Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice()));
						}
						// ///////////////////////////过路费已经其他费用////////////////////
						// 如果是市内单，则车费另外获取，在PriceDetail里面
						if (price_detail != null
								&& mMyOrderList.getOrder_type() == Value.ORDER_TYPE_SN) {
							fare = Double.parseDouble(FormatUtil
									.onFormatPrice(price_detail
											.getMetered_fee()));
						}
						//优惠劵类型
						int coupon_type = CouponUtils.coupon_type_0;
						try {
							coupon_type = Integer.parseInt( mMyOrderList
									.getDiscount().getType());
						} catch (Exception e) {
						}
						if (price_detail != null
								&& price_detail.getActual_price() != -1) {
							coupon = price_detail.getDiscountAmt();
//							switch (coupon_type) {
//							case CouponUtils.coupon_type_0:
//							case CouponUtils.coupon_type_3:break;
//							case CouponUtils.coupon_type_1:
//								coupon=CouponUtils.getZhekouSum(mDetails.getPrice(),coupon);
//								break;
//							case CouponUtils.coupon_type_2:
//								coupon=CouponUtils.getTiyanSum(mDetails.getPrice(),coupon);
//								break;
//							}
							mMyOrderList.setPrice_Detail(price_detail);
							// 需支付
							pay = Double.parseDouble(FormatUtil
									.onFormatPrice(price_detail
											.getActual_price()))
									- discount;
							//由于订单详情获取不到像订单列表最外层actual_price，需要手动在赋值下，不然存储订单会有错误
							mMyOrderList.setActual_price(price_detail
									.getActual_price());
							//带支付状态的需支付，需要自己计算
							JSONObject json;
							try {
								String paymode = "";
								if(mMyOrderList.getDiscount()!=null&&mMyOrderList.getDiscount().getMetadata()!=null){
								json = new JSONObject(mMyOrderList.getDiscount().getMetadata());
								paymode = json.getString("paymode");
								}
								if (getYMStatus() == STATUS_PAY) {
									int sale_type = 0;
									double sale_amount=0;
									try{
										sale_type=mMyOrderList.getPrice_Detail().getSaleType();
										sale_amount=mMyOrderList.getPrice_Detail().getSaleAmount();
									    }catch(Exception e){}
									if(CouponUtils.isMarkOnline(paymode)||sale_type==UsableSale.sale_type_online){
										if(sale_type==UsableSale.sale_type_online){
											pay=pay-Double.parseDouble(FormatUtil.onFormatPrice(sale_amount));
										}else{
											pay=pay-coupon;	
										}
									}
								}
							} catch (JSONException e) {
							}
							
							// 车费总计
							total_fare = Double.parseDouble(FormatUtil
									.onFormatPrice(price_detail.getTotal()));
							// 支付方式
							if (price_detail.getPay_gate() != null&&mMyOrderList.getOrder_status() == Value.ORDER_STATUS_FINISH) {
								if (price_detail.getPay_gate().equals(
										PriceDetail.ALIPAY)) {
									pay_gate = getString(R.string.pay_way_alipay);
									isLine = PriceDetail.INT_ONLINE;
								} else if (price_detail.getPay_gate().equals(
										PriceDetail.WEIXIN)
										|| price_detail.getPay_gate().equals(
												PriceDetail.WEIXIN_JSAPI)
												|| price_detail.getPay_gate().equals(
														PriceDetail.WEIXIN_NATIVE)) {
									pay_gate = getString(R.string.pay_way_weixin);
									isLine = PriceDetail.INT_ONLINE;
								} else if (price_detail.getPay_gate().equals(
										PriceDetail.LINE)) {
									isLine = PriceDetail.INT_LINE;
									pay_gate = getString(R.string.pay_way_line);
								}else if (price_detail.getPay_gate().equals(
										PriceDetail.YUE)) {
									isLine = PriceDetail.INT_ONLINE;
									pay_gate = getString(R.string.pay_way_yue);
								}
								tv_hint_paytype.setText(pay_gate);
								setYMStatus(getYMStatus());
								if(isPaySuccuss){
									isPaySuccuss=false;
									onShowCoupon(isLine);
								}
							}
						} else {
							if (mMyOrderList.getDiscount() != null) {
								coupon = mMyOrderList
										.getDiscount().getSum();
								switch (coupon_type) {
								case CouponUtils.coupon_type_0:
								case CouponUtils.coupon_type_3:
									break;
								case CouponUtils.coupon_type_1:
									coupon=CouponUtils.getZhekouSum(mMyOrderList.getPrice(),coupon);
									break;
								case CouponUtils.coupon_type_2:
									coupon=CouponUtils.getTiyanSum(mMyOrderList.getPrice(),coupon);
									break;
								}
							}
							// 车费总计=车费
							total_fare = fare;
							pay = total_fare - discount - coupon;
						}
						if (pay < 0)
							pay = 0;
						tv_pay.setText(FormatUtil.onFormatDecimal(pay));
						if (getYMStatus() == STATUS_PAY) {
							tv_confirm.setText(String.format(
									getString(R.string.confirm_pay),
									tv_pay.getText()));
						}
						// ////////////////////////
						rb_rating_s.setRating(FormatUtil
								.onFormatStar((float) mOrderDetails
										.getDetails().getDriver_star()));
						tv_rating.setText(FormatUtil
								.onFormatStar((float) mOrderDetails
										.getDetails().getDriver_star())
								+ "");
						if (mMyOrderList.getCar_NO() != null)
							tv_car_no.setText(mMyOrderList
									.getCar_NO());
						if (mMyOrderList.getSuggest() != null)
							et_message2.setText(mMyOrderList
									.getSuggest());

						if (mMyOrderList.getValuate_status() != null
								&& Integer.parseInt(mMyOrderList
										.getValuate_status()) == (Value.VALUATE_STATUS)) {// 如果该订单已完成，则显示完成界面
							rb_rating_b.setRating(FormatUtil
									.onFormatStar((mMyOrderList
											.getStar())));
						}
					
						db_myorder.onUpdate(mMyOrderList);

						list_paydetail.clear();
						// 添加车费总计
						addPayDetail(getString(R.string.total_fare),
								total_fare + "",
								PayDetailsAdapter.TYPE_PRICE, "",true);
						// 添加优惠劵
						addCouponPayDetail(false);
						mPayDetailsAdapter.notifyDataSetChanged();
						ListViewHeight.setListViewHeightBasedOnChildren(listview_paydetail);
					}
				}

			} else {
				// 设置试图,只有isLoading=true的时候，才关闭程序，，isLoading=true则是第一次启动和评价才有，FirstLoad为第一次启动加载框才有，，其他一概不关闭
				if (isLoading && FirstLoad) {
//					finish();
				}
				ToastUtil.showToast(context, R.string.no_network);
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			// 设置试图,只有isLoading=true的时候，才关闭程序，，isLoading=true则是第一次启动和评价才有，FirstLoad为第一次启动加载框才有，，其他一概不关闭
			if (isLoading && FirstLoad) {
//				finish();
			}
			ToastUtil.showToast(context, R.string.no_network);
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOrderDetail).getJson();
			return new OrderDetailNet(context, json);
		}
	}

	/**
	 * 订单评价
	 * 
	 */
	public class OrderEvaluateTask extends BaseAsyncTask {

		private OrderEvaluate mOrderEvaluate;

		public OrderEvaluateTask(Activity context, OrderEvaluate mOrderEvaluate) {
			super(context);
			this.mOrderEvaluate = mOrderEvaluate;
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOrderEvaluate).getJson();
			return new OrderEvaluateNet(context, json);
		}

		@Override
		public void onSuccess(int status,Object object) {
			ToastUtil.showToast(context, R.string.submit_success);
			et_message2.setText(et_message.getText().toString());
			mMyOrderList.setStar(rb_rating_b.getRating() * 20);
			setYMStatus(STATUS_FINISH);
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					getOrderDetail(order_id, true);
				}
			}, 1000);

		}

		@Override
		public void onFailed(int status, String msg) {
			// TODO Auto-generated method stub
			ToastUtil.showToast(context,
					getResources().getString(R.string.no_network));
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 费用明细
			if (tv_title.getText().toString().trim()
					.equals(getString(R.string.cost_detail))) {
				dragLayout.close();
			} else {
				if (contentView.getChildCount() == 0) {
					finish();
				} else {
					contentView.onNextView();
					dataList.remove(list.size() - 1);
					payList.remove(list.size() - 1);
					statusList.remove(list.size() - 1);
					successList.remove(list.size() - 1);
				}
			}
			return true;

		}
		return false;
	}

	@Override
	public void onClick(final View v) {
		switch (v.getId()) {
		case R.id.tv_right:
			if (contentView.getChildCount() == 0) {
				Tel.onTel_KF(context, mMyOrderList);
			} else {
				Tel.onTel_KF(context,
						contentView.getChild(contentView.getChildCount() - 1)
								.getMyOrderList());
			}
			break;
		case R.id.iv_left:
			// 费用明细
			if (tv_title.getText().toString().trim()
					.equals(getString(R.string.cost_detail))) {
				dragLayout.close();
			} else {
				if (contentView.getChildCount() == 0) {
					finish();
				} else {
					contentView.onNextView();
					dataList.remove(list.size() - 1);
					payList.remove(list.size() - 1);
					statusList.remove(list.size() - 1);
					successList.remove(list.size() - 1);
				}
			}

			break;
		case R.id.et_message:

			break;
		case R.id.tv_cost_detail:
			// if(include_wait.getVisibility()==View.VISIBLE){
			// include_wait.setVisibility(View.GONE);
			// test.setVisibility(View.GONE);
			// }else{
			// if(include_wait.getVisibility()==View.GONE){
			// include_wait.setVisibility(View.VISIBLE);
			// test.setVisibility(View.VISIBLE);
			// }
			// }
			// onShowPopView();
			// dragLayout.setVisibility(View.VISIBLE);
			dragLayout.open();
			dragLayout.setTime(DateFormat.onDateRemove_ss(mMyOrderList
					.getAppoint_time()));
			switch (order_type) {
			case Value.ORDER_TYPE_SN:
				dragLayout.setOrderType(getString(R.string.sn));
				dragLayout.setCountVisibility(View.GONE);
				break;
			case Value.ORDER_TYPE_PC:
				dragLayout.setOrderType(getString(R.string.pc));
				dragLayout.setCount(String.format(getString(R.string.num),
						mMyOrderList.getLocations().getCount()));
				break;
			case Value.ORDER_TYPE_BC:
				dragLayout.setOrderType(getString(R.string.bc));
				dragLayout.setCount(String.format(getString(R.string.num_car),
						mMyOrderList.getLocations().getCount()));
				break;
			case Value.ORDER_TYPE_KJ:
				dragLayout.setOrderType(getString(R.string.kj));
				// dragLayout.setCount(String.format(getString(R.string.num_jian),
				// mMyOrderList.getLocations().getCount()));
				break;
			}
			//显示爱心直通车
			if(mMyOrderList.getOrder_origin()==Value.ORDER_ORIGIN){
				dragLayout.setOrderType(getString(R.string.aixin));
				dragLayout.setCountVisibility(View.GONE);
			}
			//顺风车
			if(mMyOrderList.getOrder_origin()==Value.ORDER_ORIGIN_SF){
				dragLayout.setOrderType(getString(R.string.sf));
			}
			dragLayout.setStart(mMyOrderList.getLocations().getStart()
					.getAddress());
			dragLayout
					.setEnd(mMyOrderList.getLocations().getEnd().getAddress());
			dragLayout.setPay(pay, getYMStatus() != STATUS_PAY, pay_gate);
			try{
			// 更详细的费用明细。
			list_paydetail_more.clear();
			int calc_type=mMyOrderList.getCalcType();
			//倍率
			double base_discount=Double.parseDouble(mMyOrderList.getPrice_Detail().getBaseDiscount());
			//如果是计费，则显示套餐费
			if(calc_type==CarType.CALC_TYPE_jifei){		
				double combo_price=mMyOrderList.getPrice_Detail().getCombo_price();
				double mile_price=mMyOrderList.getPrice_Detail().getMile_price();
				double time_price=mMyOrderList.getPrice_Detail().getTime_price();
				// 套餐费
				try{
				if(combo_price!=0)
				addPayDetail_More(getString(R.string.price_combo),FormatUtil.onFormatPrice(combo_price),
						PayDetailsAdapter.TYPE_PRICE, "");
				}catch(Exception e){}
				// 超里程费
				try{
			    if(mile_price!=0)
				addPayDetail_More(getString(R.string.price_mile),FormatUtil.onFormatPrice(mile_price),
						PayDetailsAdapter.TYPE_PRICE, "");
				}catch(Exception e){}
				// 超时长费
				try{
				if(time_price!=0)
				addPayDetail_More(getString(R.string.price_time),FormatUtil.onFormatPrice(time_price),
						PayDetailsAdapter.TYPE_PRICE, "");
				}catch(Exception e){}
				
				//打折后的价格  ，转化为元 
				double totle=Double.parseDouble(FormatUtil.onFormatCeil((combo_price+mile_price+time_price)*(base_discount/100)));
				//原价  ，转化为元 
				double base_price=Double.parseDouble(FormatUtil.onFormatPrice(combo_price+mile_price+time_price));
				//调价N倍
				if(base_discount==100){
				}else{
				   String str_discount="";
				   if(base_discount>100)str_discount="加价"+(base_discount/100)+"倍";
				   else if(base_discount<100)str_discount="活动折扣"+(base_discount/10)+"折";
					
				  addPayDetail_More(str_discount, FormatUtil.onFormatDecimal(Math.abs(totle-base_price)),
						  base_discount>100?PayDetailsAdapter.TYPE_PRICE:PayDetailsAdapter.TYPE_REDUCE, "");
				}
			}
			//普通计价
			else{
				//原价，，转化为元
				double base_price=0;
				if ( mMyOrderList.getOrder_type() == Value.ORDER_TYPE_SN) {
					base_price=Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice_Detail().getMetered_fee()));

				}else{
					if(base_discount==100){
						base_price=Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice()));
					}else{
						base_price=Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice_Detail().getBasePrice()));
					}
					
				}
				// 添加车费原价
				   addPayDetail_More(context.getString(R.string.fare), FormatUtil.onFormatDecimalCeil(base_price),
						 PayDetailsAdapter.TYPE_PRICE, "");
				//调价N倍
				if(base_discount==100){
				}else{
				   String str_discount="";
				   if(base_discount>100)str_discount="加价"+(base_discount/100)+"倍";
				   else if(base_discount<100)str_discount="活动折扣"+(base_discount/10)+"折";
					
				  //打折后的价格  ，base_price已经是元了，不需要转换
				  double totle=Double.parseDouble(FormatUtil.onFormatDecimalCeil(base_price*(base_discount/100)));
				  addPayDetail_More(str_discount,FormatUtil.onFormatDecimal(Math.abs(totle-base_price)),
						  base_discount>100?PayDetailsAdapter.TYPE_PRICE:PayDetailsAdapter.TYPE_REDUCE, "");
				}
			}
			// 添加高速费
			addPayDetail_More(
					getString(R.string.highwey_fee),
					((mMyOrderList.getPrice_Detail() == null) ? "0"
							: FormatUtil.onFormatPrice(mMyOrderList
									.getPrice_Detail().getHighwey_fee())),
					PayDetailsAdapter.TYPE_PRICE, "");
			// 行李费
			addPayDetail_More(
					getString(R.string.package_fee),
					((mMyOrderList.getPrice_Detail() == null) ? "0"
							: FormatUtil.onFormatPrice(mMyOrderList
									.getPrice_Detail().getPackage_fee())),
					PayDetailsAdapter.TYPE_PRICE, "");
			// 带小孩
			addPayDetail_More(
					getString(R.string.child_fee),
					((mMyOrderList.getPrice_Detail() == null) ? "0"
							: FormatUtil.onFormatPrice(mMyOrderList
									.getPrice_Detail().getChild_fee())),
					PayDetailsAdapter.TYPE_PRICE, "");
			// 超距离
			addPayDetail_More(
					getString(R.string.over_distance),
					((mMyOrderList.getPrice_Detail() == null) ? "0"
							: FormatUtil.onFormatPrice(mMyOrderList
									.getPrice_Detail().getOver_distance())),
					PayDetailsAdapter.TYPE_PRICE, "");
			int schedule_fee_id = 0;
			switch(VersionUtils.getVersionType(context)){
			case bbx:
				schedule_fee_id=R.string.schedule_fee;
				break;
			case tianshang:
				schedule_fee_id=R.string.schedule_fee2;
				break;
			}
			//调度费
			addPayDetail_More(
					getString(schedule_fee_id),
					((mMyOrderList.getPrice_Detail() == null) ? "0"
							: FormatUtil.onFormatPrice(mMyOrderList
									.getPrice_Detail().getSchedule_fee())),
					PayDetailsAdapter.TYPE_SCHEDULE, "");
			// 添加优惠劵
			addCouponPayDetail(true);
			// 添加第三方折扣
			addPayDetail_More(list.get(pay_id).get(PayAdapter.TEXT)
					+ getString(R.string.reduce), discount + "",
					PayDetailsAdapter.TYPE_REDUCE, "");
			dragLayout.setPayDetailList(list_paydetail_more);
			}catch(Exception e){}
			break;
		case R.id.btn_coupon:
			mSharePopView.onShow(btn_coupon, Value.TYPE_SHARE, order_id,DateFormat.onDate_Long(mMyOrderList.getOrder_time()));
			break;
		case R.id.ib_sj:
			// 用intent启动拨打电话
			context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
					.parse("tel:" + mMyOrderList.getDriver_phone())));
			break;
		default:
			super.onClick(v);
			break;
		}

	}

	/**
	 * 显示状态，分别为，支付状态，评价状态，完成状态
	 */
	public void showStatus() {
		switch (getYMStatus()) {
		// 支付状态
		case STATUS_PAY:
			// 支付信息的显隐性
			tv_need_pay.setText(R.string.need_pay);
			layout_payway.setVisibility(View.VISIBLE);
			layout_hint_paytype.setVisibility(View.GONE);

			setBtnCouponVisibility(false);
			listview_pay.setVisibility(View.VISIBLE);
			layout_cost.setVisibility(View.VISIBLE);
			layout_evaluate.setVisibility(View.GONE);
			layout_cost.setVisibility(View.VISIBLE);
			et_message.setVisibility(View.GONE);
			et_message2.setVisibility(View.GONE);
			layout_anonymous.setVisibility(View.GONE);
			rb_rating_b.setIsIndicator(false);
			rb_rating_b.setRating(0);
			text_offline_payment.setVisibility(View.VISIBLE);
			break;
		// 待评价状态
		case STATUS_EVA:
			// 支付信息的显隐性
			tv_need_pay.setText(R.string.has_pay);
			layout_payway.setVisibility(View.GONE);
			layout_hint_paytype.setVisibility(View.VISIBLE);
			tv_hint_paytype.setText(pay_gate);

			setBtnCouponVisibility(true);
			listview_pay.setVisibility(View.GONE);
			layout_cost_detail.setVisibility(View.VISIBLE);
			layout_cost.setVisibility(View.VISIBLE);
			layout_evaluate.setVisibility(View.VISIBLE);
			et_message.setVisibility(View.GONE);
			layout_anonymous.setVisibility(View.VISIBLE);
			text_offline_payment.setVisibility(View.GONE);
			break;
		// 正在评价状态
		case STATUS_EVAING:
			// 支付信息的显隐性
			tv_need_pay.setText(R.string.has_pay);
			layout_payway.setVisibility(View.GONE);
			layout_hint_paytype.setVisibility(View.GONE);

			setBtnCouponVisibility(false);
			layout_evaluate.setVisibility(View.VISIBLE);
			listview_pay.setVisibility(View.GONE);
			layout_cost_detail.setVisibility(View.GONE);
			layout_cost.setVisibility(View.GONE);
			et_message.setVisibility(View.VISIBLE);
			layout_anonymous.setVisibility(View.VISIBLE);
			text_offline_payment.setVisibility(View.GONE);

			rb_rating_b.setStepSize(1);
			new Handler().postAtTime(new Runnable() {
				@Override
				public void run() {
					rb_rating_b.setIsIndicator(false);
				}
			}, 200);
			break;
		// 完成状态
		case STATUS_FINISH:
			// 支付信息的显隐性
			tv_need_pay.setText(R.string.has_pay);
			layout_payway.setVisibility(View.GONE);
			layout_hint_paytype.setVisibility(View.VISIBLE);
			tv_hint_paytype.setText(pay_gate);

			setBtnCouponVisibility(true);
			layout_evaluate.setVisibility(View.VISIBLE);
			listview_pay.setVisibility(View.GONE);
			layout_cost_detail.setVisibility(View.VISIBLE);
			layout_cost.setVisibility(View.VISIBLE);
			et_message.setVisibility(View.GONE);
			layout_anonymous.setVisibility(View.VISIBLE);
			et_message2.setVisibility(View.VISIBLE);
			layout_anonymous.setVisibility(View.VISIBLE);
			et_message2.setText(et_message.getText().toString());
			rb_rating_b.setIsIndicator(true);
			isTouch = false;
			rb_rating_b.setStepSize(0.1F);
			rb_rating_b.setRating(FormatUtil.onFormatStar(mMyOrderList
					.getStar()));
			text_offline_payment.setVisibility(View.GONE);
			break;
		}
	}

	/**
	 * 设置状态
	 */
	public void setYMStatus(int ym_status) {
		this.ym_status = ym_status;
		onSetTitle(getYMStatus());
		switch (getYMStatus()) {
		// 支付状态
		case STATUS_PAY:
			// mMyOrderList.setValuate_status(Value.PAY_NO+"");
			tv_confirm.setText(String.format(getString(R.string.confirm_pay),
					tv_pay.getText()));
			ib_sj.setVisibility(View.VISIBLE);
			break;
		// 待评价状态
		case STATUS_EVA:
			mMyOrderList.setValuate_status(Value.VALUATE_STATUS_NO + "");
			tv_confirm.setText(R.string.agin_xiadan);
			ib_sj.setVisibility(View.GONE);
			break;
		// 正在评价状态
		case STATUS_EVAING:
			mMyOrderList.setValuate_status(Value.VALUATE_STATUS_NO + "");
			tv_confirm.setText(R.string.submit_evaluation);
			ib_sj.setVisibility(View.GONE);
			break;
		// 完成状态
		case STATUS_FINISH:
			mMyOrderList.setValuate_status(Value.VALUATE_STATUS + "");
			tv_confirm.setText(R.string.agin_xiadan);
			ib_sj.setVisibility(View.GONE);
			break;
		}
		showStatus();
		db_myorder.onUpdate(mMyOrderList);
		onAixinActivity();
	}

	/**
	 * 获取当前页面状态，
	 */
	public int getYMStatus() {
		return ym_status;
	}

	private int id = -1;

	@Override
	protected void onDestroy() {
		jmsg_data = null;
		if (mOrderEvaluateTask != null
				&& mOrderEvaluateTask.getStatus() != AsyncTask.Status.FINISHED)
			mOrderEvaluateTask.cancel(true);
		if (mOrderDetailTask != null
				&& mOrderDetailTask.getStatus() != AsyncTask.Status.FINISHED)
			mOrderDetailTask.cancel(true);
		super.onDestroy();
	}

	@Override
	public void finish() {
		ArrayList<String> lits_order_id2 = new ArrayList<String>();
		if (lits_order_id.size() > 0) {
			lits_order_id2.add(lits_order_id.get(0));
			if (id >= 0)
				for (int i = 0; i <= id; i++) {
					lits_order_id2.add(lits_order_id.get(i + 1));
				}
		}
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putStringArrayList(OrderMsg.lits_order_id, lits_order_id2);
		intent.putExtras(bundle);
		setResult(resultCode, intent);
		super.finish();
	}

	@Override
	public void onNotify(int type, JSONObject jb, Msgs.Data msgs_data) {
		if (!isonNotify)
			return;
		/** 是否继续执行父类 */
		boolean isContinue = true;
		String order_id = null;
		switch (type) {
		case GMsg.ONLINE_OFFCAR:
			isContinue = false;

			try {
				JSONObject json_jmsg = jb.getJSONObject(GMsg.msg_jmsg);
				JSONObject json_data = new JSONObject(
						json_jmsg.getString(GMsg.msg_data));
				order_id = json_data.getString(GMsg.msg_order_id);
			} catch (JSONException e1) {
			}
			if (order_id != null) {
				addCardView(order_id);
			}
			break;
		case GMsg.ONLINE_FINISH:
			jmsg_data = "";
			if (msgs_data != null) {
				int card = -1;
				order_id = msgs_data.getOrder_id();
				if (contentView.getChildCount() > 0) {
					for (int i = 0; i < contentView.getChildCount(); i++) {
						String order_id_list = contentView.getChild(i)
								.getOrderId();
						if (order_id.equals(order_id_list)) {
							card = i;
							break;
						}
					}
					if (card != -1) {
						contentView.setPaySuccuss(card);
					} else {
						if (order_id.equals(this.order_id)) {
							onPaySuccuss();
						}
					}
				} else {
					if (order_id.equals(this.order_id)) {
						onPaySuccuss();
					}
				}
			}

			break;
		//改价	
		case GMsg.ONLINE_KF_CHANGE:
			ToastUtil.showToast(context, R.string.toast_change);
			jmsg_data = "";
			if (msgs_data != null) {
				int card = -1;
				order_id = msgs_data.getOrder_id();
				if (contentView.getChildCount() > 0) {
					for (int i = 0; i < contentView.getChildCount(); i++) {
						String order_id_list = contentView.getChild(i)
								.getOrderId();
						if (order_id.equals(order_id_list)) {
							card = i;
							break;
						}
					}
					if (card != -1) {
						contentView.setOrderDetail(card, order_id);
					} else {
						if (order_id.equals(this.order_id)) {
							getOrderDetail(order_id, true);
						}
					}
				} else {
					if (order_id.equals(this.order_id)) {
						getOrderDetail(order_id, true);
					}
				}
				if (tv_title.getText().toString().trim()
						.equals(getString(R.string.cost_detail))) {
					dragLayout.close();
				}	
			}
			break;
		//改价跟改单
		case GMsg.ONLINE_KF_CHANGE_ORDER:
		case GMsg.ONLINE_KF_CHANGE_PRICE:
		case GMsg.ONLINE_KF_CHANGE_PRICE2:
			isContinue = false;
			jmsg_data = "";
			if (msgs_data != null) {
				int card = -1;
				order_id = msgs_data.getOrder_id();
				if (contentView.getChildCount() > 0) {
					for (int i = 0; i < contentView.getChildCount(); i++) {
						String order_id_list = contentView.getChild(i)
								.getOrderId();
						if (order_id.equals(order_id_list)) {
							card = i;
							break;
						}
					}
					if (card != -1) {
						contentView.setOrderDetail(card, order_id);
					} else {
						if (order_id.equals(this.order_id)) {
							getOrderDetail(order_id, true);
						}else{
							//订单不在完成界面
							isContinue = true;
						}
					}
				} else {
					if (order_id.equals(this.order_id)) {
						getOrderDetail(order_id, true);
					}else{
						//订单不在完成界面
						isContinue = true;
					}
				}
			}
			break;
		}
		if (isContinue) {
			super.onNotify(type, jb, msgs_data);
		}
	}
	@Override
	public void onNotifyData(DATA_STATUS data_status, DATA_TYPE data_type, int status, Object object) {
		switch (data_type) {
			case BALANCE:
				onSetPayList();
				break;
		}
		super.onNotifyData(data_status, data_type, status, object);
	}
	@Override
	public void onDragViewStatus(boolean isOpen) {
		if (isOpen) {
			setOnCloseSwipeLayout(true);
			setTitle(getString(R.string.cost_detail));
		} else {
			if (contentView.getChildCount() == 0) {
				setOnCloseSwipeLayout(false);
				onSetTitle(getYMStatus());
			} else {
				int mstatus = contentView.getChild(
						contentView.getChildCount() - 1).getYMStatus();
				onSetTitle(mstatus);
			}
			onAixinActivity();
		}

	}


	/**
	 * @param name  名字
	 * @param money 价钱
	 * @param type  类型
	 * @param mark  标记1,t通过传递paymode进行解析。
	 */
	public void addPayDetail(String name, String money, String type, String mark,boolean discount) {
		try{
        //mark
		//如果是线上专享却是点击线下支付，则不显示
		String str_mark ="";
		if(isLine!=PriceDetail.INT_LINE){
			str_mark= CouponUtils.getMark(context, mark, false);
		 }
		if(str_mark!=null&&!str_mark.equals("")&&!list.get(pay_id).get(PayAdapter.TYPE)
				.equals(PayAdapter.TYPE_LINE)){
		}else{
			str_mark="";
		}
		//打折
		String str_discount ="";
		double base_discount=Double.parseDouble(mMyOrderList.getPrice_Detail().getBaseDiscount());
		if(base_discount>100&&discount){
			str_discount=""+base_discount/100+"倍";
		}else if(base_discount<100&&discount){
			str_discount=""+FormatUtil.onFormatDecimal2(base_discount/10)+"折";
		}
		addPayDetail(name, money, type, str_mark, str_discount);
		}catch(Exception e){}
	}
	private void addPayDetail(String name, String money, String type, String mark,String base_discount) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(PayDetailsAdapter.PAYDETAIL_NAME, name);
		map.put(PayDetailsAdapter.PAYDETAIL_MONEY, money);
		map.put(PayDetailsAdapter.PAYDETAIL_TYPE, type);
		map.put(PayDetailsAdapter.PAYDETAIL_MARK, mark);
		map.put(PayDetailsAdapter.PAYDETAIL_DISCOUNT, base_discount);
		list_paydetail.add(map);
	}
	/**
	 * 添加更加费用明细列表，及跳转到别的页面
	 */
	public void addPayDetail_More(String name, String money, String type,
			String mark) {
		try{
		String str_mark ="";
		if(isLine!=PriceDetail.INT_LINE){
		str_mark= CouponUtils.getMark(context, mark, false);
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put(PayDetailsAdapter.PAYDETAIL_NAME, name);
		map.put(PayDetailsAdapter.PAYDETAIL_MONEY, money);
		map.put(PayDetailsAdapter.PAYDETAIL_TYPE, type);
		//如果是线上专享却是点击线下支付，则不显示
		if(str_mark!=null&&!str_mark.equals("")&&!list.get(pay_id).get(PayAdapter.TYPE)
				.equals(PayAdapter.TYPE_LINE)){
		map.put(PayDetailsAdapter.PAYDETAIL_MARK, str_mark);
		}
		list_paydetail_more.add(map);
		}catch(Exception e){}
	}

	public void addCouponPayDetail(boolean isMore) {
		int sale_type=0;
		try{sale_type=mMyOrderList.getPrice_Detail().getSaleType();}catch(Exception e){}
		//获取是优惠劵还是立减
		String str_coupon="";
		switch(sale_type){
		case UsableSale.sale_type_no:
			str_coupon=getString(R.string.end_coupon);
			break;
		case UsableSale.sale_type_submit:
			str_coupon="下单立减优惠";
			try{coupon=Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice_Detail().getSaleAmount()));}catch(Exception e){}
			break;
		case UsableSale.sale_type_online:
			str_coupon="线上支付立减优惠";
			try{coupon=Double.parseDouble(FormatUtil.onFormatPrice(mMyOrderList.getPrice_Detail().getSaleAmount()));}catch(Exception e){}
			break;
		}
		
		// 添加优惠劵
		if (mMyOrderList.getDiscount() != null
				&& mMyOrderList.getDiscount().getMetadata() != null) {
			JSONObject json;
			try {
				json = new JSONObject(mMyOrderList.getDiscount().getMetadata());
				String paymode = "";
				if (getYMStatus() == STATUS_PAY) {
					paymode = json.getString("paymode");
				}
				//现金支付，却是线上专享则不添加，其他则可以添加
				if((sale_type==UsableSale.sale_type_online||CouponUtils.isMarkOnline(paymode))&&list.get(pay_id).get(PayAdapter.TYPE)
						.equals(PayAdapter.TYPE_LINE)){
					return;
				}				
					if (isMore) {
						addPayDetail_More(str_coupon, FormatUtil.onFormatDecimal(coupon), PayDetailsAdapter.TYPE_REDUCE, paymode);
					} else {
						addPayDetail(str_coupon,  FormatUtil.onFormatDecimal(coupon),
								PayDetailsAdapter.TYPE_REDUCE, paymode,false);
				}
			} catch (JSONException e) {
			}

		} else {
			//现金支付，却是线上专享则不添加，其他则可以添加
			if(sale_type==UsableSale.sale_type_online&&list.get(pay_id).get(PayAdapter.TYPE)
					.equals(PayAdapter.TYPE_LINE)){
				return;
			}
			if (isMore) {
				addPayDetail_More(str_coupon,  FormatUtil.onFormatDecimal(coupon),
						PayDetailsAdapter.TYPE_REDUCE, "");
			} else {
				addPayDetail(str_coupon,  FormatUtil.onFormatDecimal(coupon),
						PayDetailsAdapter.TYPE_REDUCE, "",false);
			}

		}
	}

	/**
	 * 设置我要优惠劵按钮的显示或隐藏
	 */
	public void setBtnCouponVisibility(boolean IsVisibility) {
		if (IsVisibility && coupon_switch == ShareDetail.STATUS_OPEN
				&& isCouponSwitch2(coupon_switch2,isLine)) {
			btn_coupon.setVisibility(View.VISIBLE);
		} else {
			btn_coupon.setVisibility(View.GONE);
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = mSharePopView.getController().getConfig()
				.getSsoHandler(requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

	public int coupon_switch;// 第一个开关是判断是否停用
	public int coupon_switch2;// 第二个开关是判断是线下用还是先上用、、
	public int isLine = PriceDetail.INT_NOLINE;;// 判断是线上还是线下，1为线上，2为线下
	private String coupon_count;
	private String coupon_sum;

	@Override
	public void onShareDetaiFinish(ShareDetail mShareDetail) {
		if (mShareDetail != null) {
			coupon_switch = mShareDetail.getState();
			coupon_switch2 = mShareDetail.getTimenode();
			coupon_count = mShareDetail.getTotalcount();
			coupon_sum=mShareDetail.getSum();
			if (coupon_switch == ShareDetail.STATUS_OPEN
					&& isCouponSwitch2(coupon_switch2,isLine)
					&& (getYMStatus() == STATUS_EVA || getYMStatus() == STATUS_FINISH)) {
				setBtnCouponVisibility(true);
			}
		}
	}
	/**判断Timenode是否开启*/
    public boolean isCouponSwitch2(int coupon_switch2,int isLine){
    	if(coupon_switch2==1||(coupon_switch2==2&&isLine==PriceDetail.INT_ONLINE)){
    		return true;
    	}
		return false;
    	
    }
	/**
	 * 支付完成后通知赋值,用完及时清空,重新打开页面则赋值""
	 */
	public static String jmsg_data = "";

	@Override
	public void onPayFinish(boolean isSuccess) {
		//通知卡片里的订单
		if (contentView.getChildCount() > 0) {
			for (int i = 0; i < contentView.getChildCount(); i++) {
				contentView.onPayFinish(i,isSuccess);
			}
		}
        //处理下当前页面的
		getOrderDetail(order_id, false);
		if (isSuccess) {
			Msgs.Data msgs_data = null;
			if (jmsg_data != null && !jmsg_data.equals("")) {
				msgs_data = (Msgs.Data) new JsonBuild().getData(
						Msgs.Data.class, jmsg_data.toString());
			}
			if (getYMStatus() == STATUS_PAY) {
				if (msgs_data != null) {
					onNotify(GMsg.ONLINE_FINISH, null, msgs_data);
				} else {
					tv_confirm.setText(R.string.btn_confirmation);
				}
			}
			}
	}
	/**
	 * 支付成功
	 */
	public void onPaySuccuss() {
		isPaySuccuss=true;
		if (getYMStatus() != STATUS_EVA) {
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					setYMStatus(STATUS_EVA);
					mMyOrderList.setOrder_status(Value.ORDER_STATUS_FINISH);
					db_myorder.onUpdate(mMyOrderList);
					db_myordering.onDelete(context, order_id);
					getOrderDetail(order_id, true);
				}
			}, 300);
		}
	}

	public void onShowCoupon(int isLine) {
		if (coupon_switch == ShareDetail.STATUS_OPEN
				&& isCouponSwitch2(coupon_switch2,isLine)) {
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					if (coupondialog != null && !coupondialog.isShowing()) {
						coupondialog.show();
					}
					coupondialog.setTitle(coupon_sum);
					coupondialog
							.setOnClickRightListener(new onClickRightListener() {

								@Override
								public void onClickRight() {
									coupondialog.dismiss();
									mSharePopView.onShow(btn_coupon,
											Value.TYPE_SHARE, order_id,DateFormat.onDate_Long(mMyOrderList.getOrder_time()));
								}
							});
				}
			}, 1000);
		}
	}
	public void onAixinActivity(){
		//爱心直通车隐藏
		if(mMyOrderList.getOrder_origin()==Value.ORDER_ORIGIN&&
				tv_confirm.getText().toString().equals(getString(R.string.agin_xiadan))) {
			layout_confirm.setVisibility(View.GONE);
			tv_right.setVisibility(View.GONE);
			layout_hint_paytype.setVisibility(View.GONE);
		}else{
			layout_confirm.setVisibility(View.VISIBLE);
			if(mMyOrderList.getOrder_origin()!=Value.ORDER_ORIGIN){
			  tv_right.setVisibility(View.VISIBLE);
			}else{
				tv_right.setVisibility(View.GONE);
			}
		}
	}
}
